package com.idas.comp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.idas.wsdl.XmlDocumentation;

@XmlDocumentation(value = "The Country class holds the base data for each country.")
public class Country {
	@XmlDocumentation(value = "The country code for the country")
	private String code;
	@XmlDocumentation(value = "The name of the country")
	private String name;

	public Country() {

	}

	protected Country(ResultSet rs) throws SQLException {
		code = rs.getString("code");
		name = rs.getString("name");
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static Country getCountryFromCode(Connection conn, String code)
			throws SQLException {
		String sql = "SELECT * FROM country WHERE country_code = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		Country country = null;
		ps = conn.prepareStatement(sql);
		ps.setString(1, code);
		rs = ps.executeQuery();
		while (rs.next())
			country = new Country(rs);
		rs.close();
		ps.close();
		return country;
	}
	
	public static ArrayList<Country> getAllCountries(Connection conn) throws SQLException{
		ArrayList<Country> list = new ArrayList<Country>();
		String sql = "SELECT * FROM country";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		while (rs.next()){
			Country country = new Country(rs);
			list.add(country);
		}
		rs.close();
		ps.close();
		return list;
	}

}
